#!/bin/sh

# ----------------------------------------------------------------------
# Filename:   cmd.sh
# Version:    2.0
# Date:       2013/09/22
# Author:     huijing.hei
# Email:      huijing.hei@cs2c.com.cn
# Summary:    rsh to rsh-server
# Notes:      Include EchoInfo and EchoResult
# Copyright:  China Standard Software Co., Ltd.
# History：     
#             Version 1.0, 2013/09/22
#             - The first one
# 
#             Version 2.0, 2013/11/08
#             - The second one
#             - add service functions, start_daemon, stop_daemon, restart_daemon, status_daemon
# ----------------------------------------------------------------------

##! @TODO: redirect rsh to /usr/bin/rsh
##! @AUTHOR: huijing.hei
##! @VERSION: 1.0 
export RSH="/usr/bin/rsh"

##! @TODO: redirect rcp to /usr/bin/rcp
##! @AUTHOR: huijing.hei
##! @VERSION: 1.0 
export RCP="/usr/bin/rcp"


#################################
#
# check command exists
#
#################################
function exists()
{
    until [ -z "$1" ]
    do
        if ! command -v $1  >/dev/null 2>&1; then
            echo "$1 not exists, pls check"
            return 1
        fi

        shift
    done

}


#################################
#
# check user exists
#
#################################
function user_exists()
{
    if id $1  >/dev/null 2>&1; then
        return 0
    else
        echo "user $1 not exists, pls check"
        return 1
    fi
}


#################################
#
# start, stop, restart service
#
#################################
if command -v systemctl >/dev/null 2>&1; then
    HAVE_SYSTEMCTL=1
else
    HAVE_SYSTEMCTL=0
fi

##! @TODO: start service
##! @AUTHOR: huijing.hei
##! @VERSION: 1.0 
function start_daemon()
{
        if [ $HAVE_SYSTEMCTL -eq 1 ]; then
                systemctl start $1.service > /dev/null 2>&1
        else
                service $1 start > /dev/null 2>&1
        fi
}

function stop_daemon()
{
        if [ $HAVE_SYSTEMCTL -eq 1 ]; then
                systemctl stop $1.service > /dev/null 2>&1
        else
                service $1 stop > /dev/null 2>&1
        fi
}

function status_daemon()
{
        if [ $HAVE_SYSTEMCTL -eq 1 ]; then
                systemctl status $1.service > /dev/null 2>&1
        else
                service $1 status > /dev/null 2>&1
        fi
}

function restart_daemon()
{
        if [ $HAVE_SYSTEMCTL -eq 1 ]; then
                systemctl restart $1.service > /dev/null 2>&1
        else
                service $1 restart > /dev/null 2>&1
        fi
}
